/*
 * Tint Browser for Android
 * 
 * Copyright (C) 2012 - to infinity and beyond J. Devauchelle and contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 3 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package com.cnbrowser.android.ui.fragments.homepage;

import android.database.Cursor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.cnbrowser.android.R;
import com.cnbrowser.android.providers.BookmarksWrapper;
import com.cnbrowser.android.ui.cbrowser.StartPageItemClickedListener;
import com.cnbrowser.android.utils.Constants;

public class MiddleFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
    private static final String TAG = "MiddleFragment";


    private View thisView;
    private StartPageItemClickedListener startPageItemClickedListener;


    public MiddleFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        thisView = inflater.inflate(R.layout.cb_start_page_middle_fragment, container, false);
        if (thisView == null) return thisView;
        final View toutiao1 = thisView.findViewById(R.id.toutiao1);
        toutiao1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startPageItemClickedListener.onItemClicked((String) toutiao1.getTag());
            }
        });

        final View toutiao2 = thisView.findViewById(R.id.toutiao2);
        toutiao2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startPageItemClickedListener.onItemClicked((String) toutiao2.getTag());
            }
        });

//        final View c12 = thisView.findViewById(R.id.pic_12_oclock);
//
//        c12.setOnClickListener(new View.OnClickListener() {
//            public void onClick(View v) {
//                startPageItemClickedListener.onItemClicked((String) c12.getTag());
//            }
//        });

        final View c6 = thisView.findViewById(R.id.pic_6_oclock);

        c6.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                startPageItemClickedListener.onItemClicked((String) c6.getTag());
            }
        });

//        final View c3 = thisView.findViewById(R.id.pic_3_oclock);
//
//        c3.setOnClickListener(new View.OnClickListener() {
//            public void onClick(View v) {
//                startPageItemClickedListener.onItemClicked((String) c3.getTag());
//            }
//        });
//
//        final View c9 = thisView.findViewById(R.id.pic_9_oclock);
//
//        c9.setOnClickListener(new View.OnClickListener() {
//            public void onClick(View v) {
//                startPageItemClickedListener.onItemClicked((String) c9.getTag());
//            }
//        });
        return thisView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        thisView.postDelayed(new Runnable() {

            @Override
            public void run() {
                if (isAdded()) {
                    getLoaderManager().initLoader(0, null, MiddleFragment.this);
                }
            }
        }, 100);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        int limit;
        try {
            limit = Integer.parseInt(
                    PreferenceManager.getDefaultSharedPreferences(getActivity()).getString(
                            Constants.PREFERENCE_START_PAGE_LIMIT,
                            Integer.toString(getResources().getInteger(R.integer.default_start_page_items_number))));
        } catch (Exception e) {
            limit = getResources().getInteger(R.integer.default_start_page_items_number);
        }

        return BookmarksWrapper.getCursorLoaderForStartPageMiddle(getActivity(), limit);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {

    }

    @Override
    public void onLoaderReset(Loader<Cursor> cursorLoader) {

    }

    public void setOnStartPageItemClickedListener(StartPageItemClickedListener startPageItemClickedListener) {
        this.startPageItemClickedListener = startPageItemClickedListener;
    }

}
